<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class C_admin extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->lang->load('page_admin', $this->current_lang);
        $this->load->model('m_teachers');
        $this->load->model('m_groups');
        $this->load->model('m_students');
        $this->load->model('m_subjects');
        $this->load->model('m_nominate');

        $data['journal_tabs'] = $this->load->view('journal/v_journal_tabs', '', true);
        $this->view->setGlobal('head', $this->load->view('v_head', $data, true));
    }

    public function index() {
        $this->view->setTitle(lang('admin_title'));
        $this->view->setGlobal('sidebar', $this->load->view('admin/v_sidebar', '', true));
        $this->view->setGlobal('content', $this->load->view('admin/v_admin', '', true));
        $this->view->render();
    }

    public function show_groups() {
        $this->load->library(array('form_validation'));
        $this->load->helper('form');
        $this->lang->load('form_validation', $this->current_lang);
        $this->view->setTitle(lang('admin_groups_title'));
        $this->view->setGlobal('sidebar', $this->load->view('admin/v_sidebar', '', true));

        $this->form_validation->set_rules('groups[]', lang('code_group'), 'trim|required');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        if ($this->form_validation->run() == FALSE) {
            $data['groups'] = $this->m_groups->get_groups();
            $this->view->setGlobal('content', $this->load->view('admin/v_show_groups', $data, true));
        } else {
            $this->m_groups->delete_group($this->input->post('groups'));
            $data['groups'] = $this->m_groups->get_groups();
            $this->view->setGlobal('content', $this->load->view('admin/v_show_groups', $data, true));
        }

        $this->view->render();
    }

    public function add_group() {
        $this->load->library(array('form_validation'));
        $this->load->helper('form');
        $this->lang->load('form_validation', $this->current_lang);
        $this->view->setTitle(lang('admin_add_group'));
        $this->view->setGlobal('sidebar', $this->load->view('admin/v_sidebar', '', true));

        $this->form_validation->set_rules('code', lang('code_group'), 'trim|required|min_length[2]');
        $this->form_validation->set_rules('manager', lang('manager_group'), 'trim|required|min_length[3]');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        if ($this->form_validation->run() == FALSE) {
            $this->view->setGlobal('content', $this->load->view('admin/v_add_group', '', true));
        } else {
            $data['message'] = $this->m_groups->insert_group($this->input->post('code'), $this->input->post('manager'));
            $data['back_link'] = base_url() . 'journal/admin/groups';
            $this->view->setGlobal('content', $this->load->view('admin/v_status', $data, true));
        }
        $this->view->render();
    }

    public function edit_group($id = 0) {
        $this->load->library(array('form_validation'));
        $this->load->helper('form');
        $this->lang->load('form_validation', $this->current_lang);
        $this->view->setTitle(lang('admin_edit_group'));
        $this->view->setGlobal('sidebar', $this->load->view('admin/v_sidebar', '', true));

        $this->form_validation->set_rules('code', lang('code_group'), 'trim|required|min_length[2]');
        $this->form_validation->set_rules('manager', lang('manager_group'), 'trim|required|min_length[3]');

        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        if ($this->form_validation->run() == FALSE) {
            $data['name_group'] = $this->m_groups->get_one_group($id);
            $data['id_group'] = $id;
            $this->view->setGlobal('content', $this->load->view('admin/v_edit_groups', $data, true));
        } else {
            $data['message'] = $this->m_groups->edit_group($id, $this->input->post('code'), $this->input->post('manager'));
            $data['back_link'] = base_url() . 'journal/admin/groups';
            $this->view->setGlobal('content', $this->load->view('admin/v_status', $data, true));
        }

        $this->view->render();
    }

    public function show_students() {
        $this->load->library(array('form_validation'));
        $this->load->helper('form');
        $this->lang->load('form_validation', $this->current_lang);
        $this->view->setTitle(lang('admin_students_title'));
        $this->view->setGlobal('sidebar', $this->load->view('admin/v_sidebar', '', true));

        $this->form_validation->set_rules('students[]', lang('name_student'), 'trim|required');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        if ($this->form_validation->run() == FALSE) {
            $data['groups'] = $this->m_groups->get_groups();
            $data['students'] = $this->m_students->get_students();
            $this->view->setGlobal('content', $this->load->view('admin/v_show_students', $data, true));
        } else {
            $this->m_students->delete_student($this->input->post('students'));
            $data['groups'] = $this->m_groups->get_groups();
            $data['students'] = $this->m_students->get_students();
            $this->view->setGlobal('content', $this->load->view('admin/v_show_students', $data, true));
        }
        $this->view->render();
    }

    public function add_student() {
        $this->load->library(array('form_validation'));
        $this->load->helper('form');
        $this->lang->load('form_validation', $this->current_lang);
        $this->view->setTitle(lang('admin_add_student'));
        $this->view->setGlobal('sidebar', $this->load->view('admin/v_sidebar', '', true));

        $this->form_validation->set_rules('text', lang('name_student'), 'trim|required|min_length[3]');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        if ($this->form_validation->run() == FALSE) {
            $data['groups'] = $this->m_groups->get_groups();
            $this->view->setGlobal('content', $this->load->view('admin/v_add_student', $data, true));
        } else {
            $data['message'] = $this->m_students->insert_students($this->input->post('text'), $this->input->post('id_group'));
            $data['back_link'] = base_url() . 'journal/admin/students';
            $this->view->setGlobal('content', $this->load->view('admin/v_status', $data, true));
        }
        $this->view->render();
    }

    public function edit_student($id = 0) {
        $this->load->library(array('form_validation'));
        $this->load->helper('form');
        $this->lang->load('form_validation', $this->current_lang);
        $this->view->setTitle(lang('admin_edit_student'));
        $this->view->setGlobal('sidebar', $this->load->view('admin/v_sidebar', '', true));

        $this->form_validation->set_rules('name', lang('name_student'), 'trim|required|min_length[3]');
        $this->form_validation->set_rules('login', lang('admin_login'), 'trim|required|min_length[3]');
        $this->form_validation->set_rules('password', lang('admin_password'), 'trim|required|min_length[3]|matches[repassword]|md5');
        $this->form_validation->set_rules('repassword', lang('admin_repassword'), 'trim|required|min_length[3]');
        $this->form_validation->set_rules('email', lang('email'), 'trim|valid_email');
        $this->form_validation->set_rules('id_group', lang('code_group'), 'trim');

        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        if ($this->form_validation->run() == FALSE) {
            $data['groups'] = $this->m_groups->get_groups();
            $data['name_student'] = $this->m_students->get_one_student($id);
            $data['id_group'] = $this->m_students->get_group_student($id);
            $data['id_student'] = $id;
            $this->view->setGlobal('content', $this->load->view('admin/v_edit_students', $data, true));
        } else {
            $data['message'] = $this->m_students->edit_student($id, $this->input->post('name'), $this->input->post('login'), $this->input->post('password'), $this->input->post('email'), $this->input->post('id_group'));
            $data['back_link'] = base_url() . 'journal/admin/students';
            $this->view->setGlobal('content', $this->load->view('admin/v_status', $data, true));
        }

        $this->view->render();
    }

    public function show_subjects() {
        $this->load->library(array('form_validation'));
        $this->load->helper('form');
        $this->lang->load('form_validation', $this->current_lang);
        $this->view->setTitle(lang('admin_subjects_title'));
        $this->view->setGlobal('sidebar', $this->load->view('admin/v_sidebar', '', true));

        $this->form_validation->set_rules('subjects[]', lang('name_subject'), 'trim|required');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        if ($this->form_validation->run() == FALSE) {
            $data['subjects'] = $this->m_subjects->get_subjects();
            $this->view->setGlobal('content', $this->load->view('admin/v_show_subjects', $data, true));
        } else {
            $this->m_subjects->delete_subject($this->input->post('subjects'));
            $data['subjects'] = $this->m_subjects->get_subjects();
            $this->view->setGlobal('content', $this->load->view('admin/v_show_subjects', $data, true));
        }

        $this->view->render();
    }

    public function add_subject() {
        $this->load->library(array('form_validation'));
        $this->load->helper('form');
        $this->lang->load('form_validation', $this->current_lang);
        $this->view->setTitle(lang('admin_add_subject'));
        $this->view->setGlobal('sidebar', $this->load->view('admin/v_sidebar', '', true));

        $this->form_validation->set_rules('text', lang('name_subject'), 'trim|required|min_length[3]');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        if ($this->form_validation->run() == FALSE) {
            $this->view->setGlobal('content', $this->load->view('admin/v_add_subject', '', true));
        } else {
            $data['message'] = $this->m_subjects->insert_subjects($this->input->post('text'));
            $data['back_link'] = base_url() . 'journal/admin/subjects';
            $this->view->setGlobal('content', $this->load->view('admin/v_status', $data, true));
        }
        $this->view->render();
    }

    public function edit_subject($id = 0) {
        $this->load->library(array('form_validation'));
        $this->load->helper('form');
        $this->lang->load('form_validation', $this->current_lang);
        $this->view->setTitle(lang('admin_edit_subject'));
        $this->view->setGlobal('sidebar', $this->load->view('admin/v_sidebar', '', true));

        $this->form_validation->set_rules('name', lang('name_subject'), 'trim|required|min_length[3]');

        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        if ($this->form_validation->run() == FALSE) {
            $data['name_subject'] = $this->m_subjects->get_one_subject($id);
            $data['id_subject'] = $id;
            $this->view->setGlobal('content', $this->load->view('admin/v_edit_subjects', $data, true));
        } else {
            $data['message'] = $this->m_subjects->edit_subject($id, $this->input->post('name'));
            $data['back_link'] = base_url() . 'journal/admin/subjects';
            $this->view->setGlobal('content', $this->load->view('admin/v_status', $data, true));
        }

        $this->view->render();
    }

    public function show_nominate_subjects() {
        $this->load->library(array('form_validation'));
        $this->load->helper('form');
        $this->lang->load('form_validation', $this->current_lang);
        $this->view->setTitle(lang('admin_nominate_title'));
        $this->view->setGlobal('sidebar', $this->load->view('admin/v_sidebar', '', true));

        $this->form_validation->set_rules('nominations[]', lang('admin_add_nominate_subjects'), 'trim|required');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        if ($this->form_validation->run() == FALSE) {
            $data['nomination'] = $this->m_nominate->get_nominations();
            $this->view->setGlobal('content', $this->load->view('admin/v_show_nominate', $data, true));
        } else {
            $this->m_nominate->delete_nominations($this->input->post('nominations'));
            $data['nomination'] = $this->m_nominate->get_nominations();
            $this->view->setGlobal('content', $this->load->view('admin/v_show_nominate', $data, true));
        }

        $this->view->render();
    }

    public function add_nominate_subjects() {
        $this->load->library(array('form_validation'));
        $this->load->helper('form');
        $this->lang->load('form_validation', $this->current_lang);
        $this->view->setTitle(lang('admin_add_nominate_subjects'));
        $this->view->setGlobal('sidebar', $this->load->view('admin/v_sidebar', '', true));

        $this->form_validation->set_rules('id_group', lang('code_group'), 'trim|required');
        $this->form_validation->set_rules('id_subject', lang('name_subject'), 'trim|required');
        $this->form_validation->set_rules('id_teacher', lang('name_teacher'), 'trim|required');

        if ($this->form_validation->run() == FALSE) {
            $data['groups'] = $this->m_groups->get_groups();
            $data['subjects'] = $this->m_subjects->get_subjects();
            $data['teachers'] = $this->m_teachers->get_teachers();
            $this->view->setGlobal('content', $this->load->view('admin/v_add_nominate', $data, true));
        } else {
            $data['message'] = $this->m_nominate->insert_nominate($this->input->post('id_group'), $this->input->post('id_subject'), $this->input->post('id_teacher'));
            $data['back_link'] = base_url() . 'journal/admin/groups/nominate-subjects';
            $this->view->setGlobal('content', $this->load->view('admin/v_status', $data, true));
        }
        $this->view->render();
    }

    public function show_teachers() {
        $this->load->library(array('form_validation'));
        $this->load->helper('form');
        $this->lang->load('form_validation', $this->current_lang);
        $this->view->setTitle(lang('admin_teachers_title'));
        $this->view->setGlobal('sidebar', $this->load->view('admin/v_sidebar', '', true));

        $this->form_validation->set_rules('teachers[]', lang('name_teacher'), 'trim|required');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        if ($this->form_validation->run() == FALSE) {
            $data['teachers'] = $this->m_teachers->get_teachers();
            $this->view->setGlobal('content', $this->load->view('admin/v_show_teachers', $data, true));
        } else {
            $this->m_teachers->delete_teacher($this->input->post('teachers'));
            $data['teachers'] = $this->m_teachers->get_teachers();
            $this->view->setGlobal('content', $this->load->view('admin/v_show_teachers', $data, true));
        }

        $this->view->render();
    }

    public function add_teacher() {
        $this->load->library(array('form_validation'));
        $this->load->helper('form');
        $this->lang->load('form_validation', $this->current_lang);
        $this->view->setTitle(lang('admin_add_teacher'));
        $this->view->setGlobal('sidebar', $this->load->view('admin/v_sidebar', '', true));

        $this->form_validation->set_rules('text', lang('name_teacher'), 'trim|required|min_length[3]');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        if ($this->form_validation->run() == FALSE) {
            $this->view->setGlobal('content', $this->load->view('admin/v_add_teacher', '', true));
        } else {
            $data['message'] = $this->m_teachers->insert_teachers($this->input->post('text'));
            $data['back_link'] = base_url() . 'journal/admin/teachers';
            $this->view->setGlobal('content', $this->load->view('admin/v_status', $data, true));
        }
        $this->view->render();
    }

    public function edit_teacher($id = 0) {
        $this->load->library(array('form_validation'));
        $this->load->helper('form');
        $this->lang->load('form_validation', $this->current_lang);
        $this->view->setTitle(lang('admin_edit_teacher'));
        $this->view->setGlobal('sidebar', $this->load->view('admin/v_sidebar', '', true));

        $this->form_validation->set_rules('name', lang('name_teacher'), 'trim|required|min_length[3]');
        $this->form_validation->set_rules('login', lang('admin_login'), 'trim|required|min_length[3]');
        $this->form_validation->set_rules('password', lang('admin_password'), 'trim|required|min_length[3]|matches[repassword]|md5');
        $this->form_validation->set_rules('repassword', lang('admin_repassword'), 'trim|required|min_length[3]');
        $this->form_validation->set_rules('phone', lang('admin_phone'), 'trim');

        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        if ($this->form_validation->run() == FALSE) {
            $data['name_teacher'] = $this->m_teachers->get_one_teacher($id);
            $data['id_teacher'] = $id;
            $this->view->setGlobal('content', $this->load->view('admin/v_edit_teachers', $data, true));
        } else {
            $data['message'] = $this->m_teachers->edit_teacher($id, $this->input->post('login'), $this->input->post('name'), $this->input->post('login'), $this->input->post('password'), $this->input->post('phone'));
            $data['back_link'] = base_url() . 'journal/admin/teachers';
            $this->view->setGlobal('content', $this->load->view('admin/v_status', $data, true));
        }

        $this->view->render();
    }

}

/* End of file c_admin.php */
/* Location: ./application/controllers/c_admin.php */